package com.eugenema.windowGui;

import com.eugenema.runMain.StartMain;
import com.eugenema.service.UpdateService;
import com.eugenema.util.ConfigUtil;
import com.eugenema.windowGui.controller.MainController;
import com.jfoenix.assets.JFoenixResources;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.container.DefaultFlowContainer;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 界面启动器
 *
 * @author Eugenema
 * @date 2023/4/2 21:42
 */
public class MainApplication extends Application {
    /** 日志 */
    private static Logger logger = LoggerFactory.getLogger(MainApplication.class);


    /** 视图上下文对象 */
    @FXMLViewFlowContext
    private ViewFlowContext context;
    /** 主界面 */
    public static final String CONTEXT_KEY_STAGE = "stage";
    /** 场景 */
    public static final String CONTEXT_KEY_SCENE = "scene";
    /** 是否首次启动，Y为首次启动，N为否 */
    public static final String FIRST_START_KEY = "FIRST_START";
    public static final String FIRST_START = ConfigUtil.getProperty(FIRST_START_KEY);
    /** 窗口大小，默认是 500*600 */
    public static double width = 800;
    public static double height = 600;

    /**
     * 启动界面
     *
     * @author Eugenema
     * @date 2023/4/2 21:55
     *
     * @param args
     **/
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * 界面初始化
     *
     * @param stage 主窗口
     *
     * @author Eugenema
     * @date 2023/3/28 22:14
     **/
    @Override
    public void start(Stage stage) throws Exception {
        //创建主界面控制器流
        Flow flow = new Flow(MainController.class);
        DefaultFlowContainer container = new DefaultFlowContainer();
        context = new ViewFlowContext();
        context.register(CONTEXT_KEY_STAGE, stage);
        flow.createHandler(context).start(container);

        //设置窗口大小，默认是 500*600，会自动根据屏幕大小设置初始大小
        try {
            Rectangle2D bounds = Screen.getScreens().get(0).getBounds();
            width = bounds.getWidth() * 0.8;
            height = bounds.getHeight() * 0.8;
        } catch (Exception e) {
            logger.error("设置窗口初始大小失败，重置为默认值：800*600", e);
        }

        //不显示标题栏
        stage.initStyle(StageStyle.UNDECORATED);

        //建立场景并注册到上下文中
        Scene scene = new Scene(container.getView(), width, height);
        context.register(CONTEXT_KEY_SCENE, scene);

        //加载样式
        final ObservableList<String> stylesheets = scene.getStylesheets();
        stylesheets.addAll(
                JFoenixResources.load("/css/components-aspect.css").toExternalForm(),
                JFoenixResources.load("/css/components-basic.css").toExternalForm()
        );

        //窗口关闭事件
        stage.setOnCloseRequest(event -> StartMain.closeProgram());
        //不可改变窗口大小
        stage.setResizable(false);
        //设置图标
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/logo/logoPng.png")));
        stage.setTitle("THIS");

        //设置场景并显示
        stage.setScene(scene);
        stage.show();

        // 后台检查更新
        StartMain.threadPool.submit(() -> {
            UpdateService.updateCheck();
        });

        // 首次启动，弹出更新提示
        if ("Y".equals(FIRST_START)) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "欢迎使用本软件，当前为首次启动，请点击右下角【版本号】按钮获取本次更新内容");
            // 弹窗置顶
            alert.setTitle("升级成功");
            alert.setHeaderText("首次启动提示");
            alert.show();
            ConfigUtil.setPropertySingle(FIRST_START_KEY, "N");
        }
    }
}